package com.dexgdx.game.action.attack;

import com.dexgdx.game.Game;
import com.dexgdx.game.structure.actor.AbstractActor;
import com.dexgdx.game.structure.base.BaseRegion;
import com.dexgdx.game.structure.projectile.DexTrailObject;
import com.dexgdx.game.util.trajectory.TrajectoryCalculator;

public class DexTrailAttackAction extends AbstractAttackAction {
	
	private boolean initialized;

	private AbstractActor attacker;
	private BaseRegion target;
	
	private DexTrailObject dexTrailObject; 
	private int jumpCount;
	
	private TrajectoryCalculator trajectoryCalculator;
	
	public DexTrailAttackAction(AbstractActor attacker, BaseRegion target) {
		super();
		this.attacker = attacker;
		this.target = target;
		this.async = true;
	}
	
	@Override
	public boolean hasStarted() {
		return initialized;
	}
	
	@Override
	public void initialize() {
		trajectoryCalculator = new TrajectoryCalculator(40);
		BaseRegion tempRegion = attacker.cloneRegion();
		tempRegion.x += tempRegion.width/2;
		tempRegion.y += tempRegion.height/2;
		dexTrailObject = new DexTrailObject(attacker, trajectoryCalculator.calculate(tempRegion, target));
		Game.gameData.addNonActorObject(dexTrailObject);
		initialized = true;
	}

	@Override
	public void runNextStep() {
		if(jumpCount < 40){
			Game.animationRunning = true;
			dexTrailObject.loadNewPosition();
			jumpCount++;
		}
	}

	@Override
	public boolean isFinished() {
		boolean finished = false;
		if(jumpCount == 40 && dexTrailObject.areAllParticlesDead()){
			Game.gameData.removeNonActorObject(dexTrailObject);
			finished = true;
		}
		return finished;
	}

}
